<template>
  <div class="editable-element-document"
    :style="{
      top: elementInfo.top + 'px',
      left: elementInfo.left + 'px',
      width: elementInfo.width + 'px',
      height: elementInfo.height + 'px',
    }"
  >
    <div
      class="rotate-wrapper"
      :style="{ transform: `rotate(${elementInfo.rotate}deg)` }"
    >
      <div 
        class="element-content" 
        v-contextmenu="contextmenus"
        @mousedown="$event => handleSelectElement($event)"
        @touchstart="$event => handleSelectElement($event)">
       
        <iframe
          title="doc iframe"
          ref="iframe"
          :id="elementInfo.id"
          :src="elementInfo.url"
          :width="elementInfo.width"
          :height="elementInfo.height"
          :frameborder="0" 
          :allowfullscreen="true"></iframe>

        <div class="drag-handler top"></div>
        <div class="drag-handler bottom"></div>
        <div class="drag-handler left"></div>
        <div class="drag-handler right"></div>

        <div class="mask" 
          v-if="handleElementId !== elementInfo.id"
          @mousedown="$event => handleSelectElement($event, false)"
          @touchstart="$event => handleSelectElement($event, false)"
        ></div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { type PropType, onMounted, ref, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore, useSlidesStore } from '@/store'
import useHistorySnapshot from '@/hooks/useHistorySnapshot'
import type { PPTDocumentElement } from '@/types/slides'
import type { ContextmenuItem } from '@/components/Contextmenu/types'

const props = defineProps({
  elementInfo: {
    type: Object as PropType<PPTDocumentElement>,
    required: true,
  },
  selectElement: {
    type: Function as PropType<(e: MouseEvent | TouchEvent, element: PPTDocumentElement, canMove?: boolean) => void>,
    required: true,
  },
  contextmenus: {
    type: Function as PropType<() => ContextmenuItem[] | null>,
  },
})

const iframe = ref(null)

const slidesStore = useSlidesStore()
const { handleElementId } = storeToRefs(useMainStore())
const { addHistorySnapshot } = useHistorySnapshot()

// 监听scrollTop
watch([() => props.elementInfo.scrollTop], (scrollTop) => {
  // @ts-ignore
  const iframeDocument = iframe.value.contentDocument || iframe.value.contentWindow.document
  iframeDocument.documentElement.scrollTop = [scrollTop]
})

// 设置滚动效果
onMounted(() => {
  // @ts-ignore
  iframe.value.addEventListener('load', () => {
    setTimeout(() => {
      // 首次加载时滚到目标位置
      // @ts-ignore
      const iframeDocument = iframe.value.contentDocument || iframe.value.contentWindow.document
      iframeDocument.documentElement.scrollTop = props.elementInfo.scrollTop

      // 鼠标离开文档时，记录内嵌文档当前的滚动距离
      // @ts-ignore
      iframe.value.addEventListener('mouseleave', () => {
        // @ts-ignore
        const iframeDocument = iframe.value.contentDocument || iframe.value.contentWindow.document
        slidesStore.updateElement({ id: handleElementId.value, props: { scrollTop: iframeDocument.documentElement.scrollTop } })
        addHistorySnapshot()
      })
    }, 500)
  })

})

const handleSelectElement = (e: MouseEvent | TouchEvent, canMove = true) => {
  e.stopPropagation()
  props.selectElement(e, props.elementInfo, canMove)
}

</script>

<style lang="scss" scoped>
.editable-element-document {
  position: absolute;
}
.element-content {
  width: 100%;
  height: 100%;
  background:#fff;
  cursor: move;
}
.drag-handler {
  position: absolute;

  &.top {
    height: 20px;
    left: 0;
    right: 0;
    top: 0;
  }
  &.bottom {
    height: 20px;
    left: 0;
    right: 0;
    bottom: 0;
  }
  &.left {
    width: 20px;
    top: 0;
    bottom: 0;
    left: 0;
  }
  &.right {
    width: 10px;
    top: 0;
    bottom: 0;
    right: -10px;
  }
}
.mask {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}
</style>
